<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">




        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
        </script>

    </div>
</div>

<script>
    layui.use(['form', 'table','miniPage','element', 'axios'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;
        var axios = layui.axios;
     //重新加载 table
        window.reload=function(){
            table.reload('currentTableId');
        }


        table.render({
            elem: '#currentTableId',
            url: SURL+'user/queryUserAll2',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                // {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'username', width: 80, title: '用户名'},
                {field: 'sex', width: 80, title: '性别'},
                // {field: 'roleName', width: 80, title: '用户类型'},
                {field: 'roleName',width: 80, title: '角色',templet:function(d){
                    if(d.roleName==0){
                        return '<span class="layui-badge layui-bg-green" >医生</span>';
                    }else{
                        return '<span class="layui-badge  layui-bg-blue">管理员</span>';
                    }
                }},
                {templet:'<div>{{d.dept.name}}</div>',title: '科室', minWidth: 100},
                // {field: 'deptId', title: '科室', minWidth: 150},
                {field: 'tel', width: 120, title: '电话'},
                {field: 'email', width: 120, title: '邮箱'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            // limits: [10, 15, 20, 25, 50, 100],
            // limit: 15,
            // page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            // var result = JSON.stringify(data.field);
            // layer.alert(result, {
            //     title: '最终的搜索信息'
            // });
            var username=data.field.username;
            var email=data.field.email;
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    username: username,
                    email:email
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {   // 监听添加操作
                var content = miniPage.getHrefContent('page/user/add.html');
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '添加用户',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                //批量删除功能
                /*
                  1、获取要删除的记录信息 ，如果没选择 提示
                  2、获取要删除记录的id信息
                  3、监听提交事件调用后端方法
                 */
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if(data.length==0){
                    layer.msg("请选择你要删除的记录信息");
                }else{
                    //调用获取选中记录的id
                    var ids=getCheackId(data);
                    layer.confirm('真的删除行么', function (index) {
                        //像后端调用请求
                        axios.get('user/deleteByIds?ids='+ids).then(function(response){
                            debugger
                            if(response.data.code==200){
                                layer.msg(response.data.msg);
                                //重新加载表格
                                // window.reload();
                                table.reload('currentTableId');
                            }
                        }).catch(err=> {
                                console.log(err);
                            }
                        );

                    });
                }

            }
        });

        /**
         * 获取记录信息的id
         */
        function getCheackId(data){
            var arr=new Array();
            for(var i=0;i<data.length;i++){
                arr.push(data[i].id);
            }
            return arr.join(",");
        }


        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        //渲染数据 编辑
        function setFormValues(data){
            form.val('updateSubmit',{
                "id":data.id,
                "username":data.username,
                "password":data.password,
                "sex":data.sex,
                "tel":data.tel,
                "email":data.email,
                "info":data.info,
                "roleName":data.roleName,
                "jobTitle2":data.jobTitle,
                "deptId":data.deptId
            });
        }



        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                var content = miniPage.getHrefContent('page/user/update.html');
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '编辑用户',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });

                //渲染值信息
                setFormValues(data);
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    //获取要删除的id 调用相关后端接口即可
                    axios.get('user/deleteByIds?ids='+data.id).then(function(response){
                        debugger
                        if(response.data.code==200){
                            layer.msg(response.data.msg);
                            //重新加载表格
                            // window.reload();
                            table.reload('currentTableId');
                        }
                    }).catch(err=> {
                            console.log(err);
                        }
                    );


                });
            }
        });

    });
</script>